package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.CustomerService;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.CustomerDao;
import com.qdlc.p2p.dal.dto.Customer;
import com.qdlc.p2p.dal.model.CustomerModel;
/**
 * 
 * Customer 客服模块 service实现
 * @author zlhx
 * @version 2.0
 * @date 2015年8月3日 下午3:19:52
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
	@Resource
	private CustomerDao customerDao;
	
	@Override
	public Customer find(long id) {
		return customerDao.find(id);
	}

	@Override
	public Customer save(Customer customer) {
		return customerDao.save(customer);
	}

	@Override
	public Customer modify(Customer customer) {
		return customerDao.update(customer);
	}

	@Override
	public void modify(long id, int status) {
		int result = customerDao.modify(id, status);
		if (result != 1) {
			throw new UserException("更改客服状态失败！", 1);
		}
	}

	@Override
	public void delete(long id, int isDelete) {
		int result = customerDao.delete(id, isDelete);
		if (result != 1) {
			throw new UserException("删除客服失败！", 1);
		}
	}

	@Override
	public PageDataList<CustomerModel> pList(CustomerModel model) {
		QueryParam param = QueryParam.getInstance();
		if(model!=null){
			if(StringUtil.isNotBlank(model.getSearchName())){
				SearchFilter orFilter1 = new SearchFilter("userName", Operators.LIKE, model.getSearchName().trim());
        		SearchFilter orFilter2 = new SearchFilter("qq", Operators.LIKE, model.getSearchName().trim());
        		param.addOrFilter(orFilter1, orFilter2);
			}else{
				//名字 检索
				if(!StringUtil.isBlank(model.getUserName())){
					param.addParam("userName", Operators.LIKE, model.getUserName());
				}
				if(!StringUtil.isBlank(model.getQq())){
					param.addParam("qq", Operators.LIKE, model.getQq());
				}
				if(!StringUtil.isBlank(model.getMobilePhone())){
					param.addParam("mobilePhone", Operators.LIKE, model.getMobilePhone());
				}
			}
		}
		param.addParam("isDelete", CustomerModel.NOTDELETE);
		param.addOrder(OrderType.DESC, "id");
		param.addPage(model.getPage(), model.getRows());
		PageDataList<Customer> p = customerDao.findAllPageList(param);
		List<CustomerModel> list = new ArrayList<CustomerModel>();
		PageDataList<CustomerModel> plist = new PageDataList<CustomerModel>();
		for(Customer customer : p.getList()){
			CustomerModel customerModel = CustomerModel.instance(customer);
			list.add(customerModel);
		}
		plist.setPage(p.getPage());
		plist.setList(list);
		return plist;
	}
}
